//
//  SpecialDetailsTableViewController.m
//  GiftItems
//
//  Created by lanou3g on 15/9/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "SpecialDetailsTableViewController.h"
#import "SpecialDeatialsTableViewCell.h"
#import "SpecialDeatilsModel.h"

#import "MainTableViewCell.h"
#import "MainBoutiqueModel.h"
@interface SpecialDetailsTableViewController ()
@property (nonatomic,strong)NSMutableArray *specialArray;

@property (nonatomic,strong)NSString *titleTop;

@end

@implementation SpecialDetailsTableViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    if ([self.name isEqualToString:@"精心挑礼"]) {
        [self analysisWitURL];
    }else{
        [self reloadSpacialDeatial];
    }
    [self.tableView registerClass:[MainTableViewCell class] forCellReuseIdentifier:@"cell"];
    NSLog(@"cesss");
    self.tableView.separatorStyle = NO;
}

//精心挑礼界面传过来的链接 进行数据解析
- (void)analysisWitURL{
    [LORequestManger GET:self.specialDeatilsUrl success:^(id response) {
        NSDictionary *Dic = (NSDictionary *)response;
        NSDictionary *dictionary = Dic[@"data"];
        for (NSDictionary *dic in dictionary[@"items"]) {
            MainBoutiqueModel *model = [MainBoutiqueModel shareDictionary:dic];
            [self.specialArray addObject:model];
        }
        [self.tableView reloadData];
        self.navigationItem.title = self.titleName;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        
        
        
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark --- 数据解析 ---
- (void)reloadSpacialDeatial {
    [LORequestManger GET:self.specialDeatilsUrl success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *dicData = dic[@"data"];
        self.titleTop = dicData[@"title"];
        for (NSDictionary *posts in dicData[@"posts"]) {
            MainBoutiqueModel *model = [MainBoutiqueModel shareDictionary:posts];
            [self.specialArray addObject:model];
        }
        [self.tableView reloadData];
        self.navigationItem.title = self.titleTop;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (NSMutableArray *)specialArray {
    if (!_specialArray) {
        _specialArray = [[NSMutableArray alloc] init];
    }
    return _specialArray;
    
}

#pragma mark --- cell设置 ---

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.specialArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[MainTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    cell.LickCountButton.tag = indexPath.row;
    [cell.LickCountButton addTarget:self action:@selector(clickZan:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    MainBoutiqueModel *model = [[MainBoutiqueModel alloc] init];
    model = self.specialArray[indexPath.row];
    
    NSArray *modelArray = [[StrategyDataBase shareDataBase] findStrategyModelByTitle:model.title];
    MainBoutiqueModel *modelTitle = modelArray.lastObject;
    
    if (modelTitle) {
        [cell.LickCountButton setImage:[UIImage imageNamed:@"profile_btn_like"] forState:UIControlStateNormal];
    }else {
        [cell.LickCountButton setImage:[UIImage imageNamed:@"profile_btn_unlike"] forState:UIControlStateNormal];
        
    }

    [cell shareMainImageModel:model];
    return cell;
}



// 点赞
- (void)clickZan:(UIButton *)button {
    
    MainBoutiqueModel *model = self.specialArray[button.tag];
    
    CAKeyframeAnimation *k = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    k.values = @[@(0.1),@(1.0),@(1.5)];
    k.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
    k.calculationMode = kCAAnimationLinear;
    
    NSArray *modelArray = [[StrategyDataBase shareDataBase] findStrategyModelByTitle:model.title];
    MainBoutiqueModel *modelTitle = modelArray.lastObject;
    
    if (!modelTitle) {
        [button setImage:[UIImage imageNamed:@"profile_btn_like"] forState:UIControlStateNormal];
        button.tintColor = [UIColor redColor];
        [[StrategyDataBase shareDataBase] createStrategyListWithName:@"strategyList"];
        [[StrategyDataBase shareDataBase] insertModel:model];
        [[StrategyDataBase shareDataBase] showAlert];
        NSUInteger linkCount = [model.likes_count integerValue] + 1;
        [button setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)linkCount] forState:(UIControlStateNormal)];
    }else
    {
        [button setImage:[UIImage imageNamed:@"profile_btn_unlike"] forState:UIControlStateNormal];
        button.tintColor = [UIColor whiteColor];
        [[StrategyDataBase shareDataBase] createStrategyListWithName:@"strategyList"];
        [[StrategyDataBase shareDataBase] deleteModelWithStrategy:model.title];
        [[StrategyDataBase shareDataBase] deleteAlert];
        [button setTitle:[NSString stringWithFormat:@"%@",model.likes_count] forState:(UIControlStateNormal)];
        
    }
    [button.layer addAnimation:k forKey:@"SHOW"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 155;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WebViewController *webView = [[WebViewController alloc] init];
    
    SpecialDeatilsModel *model = [[SpecialDeatilsModel alloc] init];
    
    model = self.specialArray[indexPath.row];
    
    webView.webUrl = [KStrategyUrl stringByReplacingOccurrencesOfString:@"参数" withString:[NSString stringWithFormat:@"%@",model.ID]];
    webView.type = @"攻略";
    
    [self presentViewController:webView animated:YES completion:nil];
    
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end